<?php
require_once(APPPATH.'views/components/page.php');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<link href='<?php echo base_url() ?>resources/css/admin.css' rel='stylesheet' type='text/css' />
		<script src="<?php echo base_url() ?>resources/js/jquery-1.4.2.min.js"></script>
		<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
		
		<script type="text/javascript">
			window.onbeforeunload = leavingPageHandler;
		    function leavingPageHandler(e)
		    {
		         var selectall = document.getElementById('selectall');
		         selectall.checked = false;
		         selectAll(selectall);
		         
		         var e = document.getElementsByName('newstypes');
				 e[0].selectedIndex = 0;
		         
		    }
		    
			function selectAll(selectall)
			{
				var checkboxes = document.getElementsByName("newsids[]");
				for (var i = 0; i < checkboxes.length; i++)
				{
					checkboxes[i].checked = selectall.checked;
				}
				
				showDeleteBtn(selectall.checked);
			}
			
			function showDeleteBtn(show)
			{
				var deleteBtn = document.getElementById("deletebtn");
				deleteBtn.disabled = !show;
			}
			
			function onSingleCbClicked()
			{
				var checkboxes = document.getElementsByName("newsids[]");
				var count = 0;
				for (var i = 0; i < checkboxes.length; i++)
				{
					if (checkboxes[i].checked)
						count++;
				}
				showDeleteBtn(count > 0);
				
				var selectall = document.getElementById('selectall');
				selectall.checked = (count == checkboxes.length);
					
			}
			
			function onDeleteBtnClicked()
			{
				var checkboxes = document.getElementsByName("newsids[]");
				var count = 0;
				
				for (var i = 0, size = checkboxes.length; i < size; i++)
				{
					if (checkboxes[i].checked)
						count++;
				}
				
				if (count == 0)
					return false;
				
				return confirm("Xóa " + count + " Tin tức?");
			}
			
			function onViewBtnClicked(baseurl)
			{
				var e = document.getElementsByName('newstypes');
				var type = e[0].options[e[0].selectedIndex].value;
				
				window.location = baseurl + 'news/viewByType/' + type;
			}
		</script>
		
		<title>Quản lý tin tức</title>
	</head>
	
	<body>
		<div id='wrapper'>
			<div id='header'>
				<div id="leftTopPan">
				<a href='<?php echo base_url() ?>'><img src='<?php echo base_url() ?>resources/images/logo.gif' title='Consultant' alt='Consultant' width='160' height='39' border='0' /></a>
				</div>
				<h1></h1>
			</div>
			<div id="middle">
				<div id="container">
					<div id="content">
						<br /><h2 align="center">Quản lý tin tức</h2><br />
						
						<?php
						
						// Error handler
						$error = $this->session->userdata('error');
						$this->session->unset_userdata('error');
						$errorText = "";
						
						switch ($error) {
							case DELETE_ERROR:
								$errorText = sprintf(DELETE_ERROR_TEXT, 'Tin tức');
								break;
							default:
								break;
						}
						?>
						<h3 style="color: red"><?php echo $errorText ?></h3>
						<?php
						
						
						$userdata = $this->session->userdata('userdata');
						
						if (Page::getInstance()->isAdmin())
						{
							// User information
							Page::getInstance()->generateUserInfo($userdata);
							
							echo anchor("news/insert", "Thêm tin tức");
							
							// Begin delete form
							echo form_open('news/delete');
						}
						
						// News list
						// View button
						$viewBtn = array('id'	=>	'viewbtn',
										 'name'	=>	'viewbtn',
										 'content'=>	'Xem',
										 'onclick'	=> 	"onViewBtnClicked('".base_url()."')");
						
						$promotion = isset($rpromotion) ? $rpromotion : 0;
						$newsTypeArr = array('0'	=>	"Làm đẹp",
											'1'		=>	"Khuyến mãi");
						
						$selectall = array(		'id'		=>	'selectall',
												'onclick'	=>	'selectAll(this)',
												'checked'	=>	FALSE);
						
						?>
						<p><hr /></p>
						<h2 align="center">Danh sách tin tức</h2><br />
						<p>Xem tin tức: <?php echo form_dropdown('newstypes', $newsTypeArr, $promotion, 'style="width:300px"') ?><?php echo form_button($viewBtn) ?></p>
						
						<?php
						// Delete button
							$deleteBtn = array(		'id'		=>	'deletebtn',
													'name'		=>	'deletebtn',
													'value'		=>	'Xóa',
													'disabled'	=>	TRUE,
													'onclick'	=>	'return onDeleteBtnClicked()');
							echo form_submit($deleteBtn);
						?>
						
						<table id="newstable" border="1">
							<tr>
								<td width="2%"><?php echo form_checkbox($selectall) ?></td>
								<th>Tiêu đề</th>
							</tr>
						<?php
						foreach ($newsarr as $news)
						{
							?>
							<tr>
								<td><?php echo form_checkbox('newsids[]', $news->getId(), FALSE, "onclick='onSingleCbClicked()'") ?></td>
								<td><?php echo anchor('news/view/'.$news->getId(), $news->getTitle()) ?></td>
							</tr>
							<?php
						}
						?>
						</table>
						<?php echo form_close() ?>
						<?php echo $paging_link ?>
					</div>
				</div>
					<?php
						if (Page::getInstance()->isAdmin())
						{
							Page::getInstance()->generateAdminLeftMenu();
						}
					?>
			</div>
		</div>
		
	</body>
</html>